//
//  RootViewController.m
//  linkingGame
//
//  Created by zw wu on 15/4/4.
//  Copyright (c) 2015年 com.jinWu. All rights reserved.
//

#import "RootViewController.h"
#import "SettingViewController.h"
#import "GameLevelViewController.h"
#import "AddStyleViewController.h"

@interface RootViewController ()

@property (nonatomic,retain) UIButton *addStyleBtn;
@property (nonatomic,retain) UIButton *classicModeBtn;
@property (nonatomic,retain) UIButton *challengeModeBtn;
@property (nonatomic,retain) UIButton *settingsBtn;
@property (nonatomic,retain) UIButton *declarationBtn;

@end

@implementation RootViewController

- (BOOL)prefersStatusBarHidden
{
    return YES;//隐藏为YES，显示为NO
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self resetAllSubViewFrame];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)])
    {
        [self prefersStatusBarHidden];
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    }
    
    UIButton *addStyleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addStyleBtn addTarget:self action:@selector(addStyle) forControlEvents:UIControlEventTouchUpInside];
    [addStyleBtn setTitle:NSLocalizedString(@"choose_style",nil) forState:UIControlStateNormal];
    [self.view addSubview:addStyleBtn];
    self.addStyleBtn = addStyleBtn;
    
    UIButton *classicModeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [classicModeBtn addTarget:self action:@selector(showLevel:) forControlEvents:UIControlEventTouchUpInside];
    [classicModeBtn setTitle:NSLocalizedString(@"classic_mode",nil) forState:UIControlStateNormal];
    [self.view addSubview:classicModeBtn];
    self.classicModeBtn = classicModeBtn;
    
    UIButton *challengeModeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [challengeModeBtn addTarget:self action:@selector(showLevel:) forControlEvents:UIControlEventTouchUpInside];
    [challengeModeBtn setTitle:NSLocalizedString(@"challenge_mode",nil) forState:UIControlStateNormal];
    [self.view addSubview:challengeModeBtn];
    self.challengeModeBtn = challengeModeBtn;
    
    UIButton *settingsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [settingsBtn addTarget:self action:@selector(setting) forControlEvents:UIControlEventTouchUpInside];
    [settingsBtn setTitle:NSLocalizedString(@"setting",nil) forState:UIControlStateNormal];
    [self.view addSubview:settingsBtn];
    self.settingsBtn = settingsBtn;
    
    UIButton *declarationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [declarationBtn setTitle:NSLocalizedString(@"rule_declaration",nil) forState:UIControlStateNormal];
    [self.view addSubview:declarationBtn];
    self.declarationBtn = declarationBtn;
    
    addStyleBtn.backgroundColor = [UIColor redColor];
    classicModeBtn.backgroundColor = [UIColor redColor];
    challengeModeBtn.backgroundColor = [UIColor redColor];
    settingsBtn.backgroundColor = [UIColor redColor];
    declarationBtn.backgroundColor = [UIColor redColor];
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [self resetAllSubViewFrame];
}

- (void)resetAllSubViewFrame
{
    CGSize mainSize = self.view.bounds.size;
    CGRect addStyleBtnRect,classicModeBtnRect,challengeModeBtnRect,settingsBtnRect,declarationBtnRect;
    if (isPhone())
    {
        addStyleBtnRect = CGRectMake((int)((mainSize.width - 240)/2), (int)(mainSize.height - 360), 240, 40);
        classicModeBtnRect = CGRectMake((int)((mainSize.width - 240)/2), (int)(mainSize.height - 300), 240, 40);
        challengeModeBtnRect = CGRectMake((int)((mainSize.width - 240)/2), (int)(mainSize.height - 240), 240, 40);
        settingsBtnRect = CGRectMake((int)((mainSize.width - 240)/2), (int)(mainSize.height - 180), 240, 40);
        declarationBtnRect = CGRectMake((int)((mainSize.width - 240)/2), (int)(mainSize.height - 120), 240, 40);
    }
    else
    {
        addStyleBtnRect = CGRectMake((int)((mainSize.width - 360)/2), (int)(mainSize.height - 620), 360, 60);
        classicModeBtnRect = CGRectMake((int)((mainSize.width - 360)/2), (int)(mainSize.height - 520), 360, 60);
        challengeModeBtnRect = CGRectMake((int)((mainSize.width - 360)/2), (int)(mainSize.height - 420), 360, 60);
        settingsBtnRect = CGRectMake((int)((mainSize.width - 360)/2), (int)(mainSize.height - 320), 360, 60);
        declarationBtnRect = CGRectMake((int)((mainSize.width - 360)/2), (int)(mainSize.height - 220), 360, 60);
    }
    
    self.addStyleBtn.frame = addStyleBtnRect;
    self.classicModeBtn.frame = classicModeBtnRect;
    self.challengeModeBtn.frame = challengeModeBtnRect;
    self.settingsBtn.frame = settingsBtnRect;
    self.declarationBtn.frame = declarationBtnRect;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    self.addStyleBtn = nil;
    self.classicModeBtn = nil;
    self.challengeModeBtn = nil;
    self.settingsBtn = nil;
    self.declarationBtn = nil;
    
    [super dealloc];
}

- (void)addStyle
{
    AddStyleViewController *addStyleVC = [[AddStyleViewController alloc]init];
    [self.navigationController pushViewController:addStyleVC animated:NO];
    [addStyleVC release];
}

- (void)setting
{
    SettingViewController *settingVC = [[SettingViewController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:settingVC];
    [settingVC release];
    
    [self.navigationController presentViewController:nav animated:YES completion:nil];
    [nav release];
}

- (void)showLevel:(UIButton *)sender
{
    KGameMdoe gameMode = KClassicMode;
    if (sender == self.challengeModeBtn)
    {
        gameMode = KChallengeMode;
    }
    
    GameLevelViewController *levelVC = [[GameLevelViewController alloc]init];
    levelVC.gameMode = gameMode;
    [self.navigationController pushViewController:levelVC animated:NO];
    [levelVC release];
}

@end
